package cn.com.czpb.client.module.notepad;

import android.app.AlertDialog;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.BaseFragment;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.notepad.NotePadChangeEvent;
import cn.com.czpb.client.model.notepad.NotePdeBean;
import cn.com.czpb.client.module.notepad.adapter.NotePadAdapter;
import cn.com.czpb.client.utils.LogUtils;
import cn.com.czpb.client.utils.PageCtrl;
import cn.com.czpb.client.utils.TUtils;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * Created by Administrator on 2018/6/21.
 */

public class NotePadFragment extends BaseFragment implements BaseQuickAdapter.OnItemChildClickListener, OnRefreshListener {
    @BindView(R.id.recycler_id)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh_id)
    SmartRefreshLayout refresh_id;

    NotePadAdapter mAdapter;


    private int ADD_NOTEPAD = 0;
    private int CHANGE_NOTEPAD = 1;

    @Override
    public int setMyContentView() {
        return R.layout.fragment_notepad;
    }


    public static final NotePadFragment newInstance() {
        NotePadFragment fragment = new NotePadFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void initView() {

    }

    @Override
    public void initData() {
        EventBus.getDefault().register(this);
        initAdapter();
    }

    private void initAdapter() {
        refresh_id.setRefreshHeader(new ClassHeader_other(activity));
        mAdapter = new NotePadAdapter(null);
        GridLayoutManager manager = new GridLayoutManager(activity, 3);
//        manager.setStackFromEnd(true);
//        manager.setReverseLayout(true);
        mRecyclerView.setLayoutManager(manager);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(this);
        refresh_id.setOnRefreshListener(this);
        initList();
    }

    private void initList() {
        List<NotePdeBean> notePdeBeanList = LitePal.order("id desc").find(NotePdeBean.class);
//
        for (int i = 0; i < notePdeBeanList.size(); i++) {
            LogUtils.i("bean----" + notePdeBeanList.get(i).toString());
        }
        NotePdeBean bean = new NotePdeBean();

        bean.setType(0);

//        list.add(bean);
        notePdeBeanList.add(0, bean);
        mAdapter.setNewData(notePdeBeanList);
        mAdapter.notifyDataSetChanged();
    }


    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

        NotePdeBean bean = (NotePdeBean) adapter.getItem(position);
        switch (view.getId()) {
            case R.id.item:
                if (bean.getType() == 0) {
                    PageCtrl.start2InputNotepadActivity(activity, bean.getTime(), bean.getContent(), ADD_NOTEPAD, bean.getId());
                } else {
                    PageCtrl.start2InputNotepadActivity(activity, bean.getTime(), bean.getContent(), CHANGE_NOTEPAD, bean.getId());
                }
                break;
            case R.id.iv_delete:
                LitePal.delete(NotePdeBean.class, bean.getId());
                initList();
                break;
        }
    }

    @Subscribe
    public void onEventMainThread(NotePadChangeEvent event) {
        LogUtils.i("event-----" + event.isChange());
        if (event.isChange()) {
            initList();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                initList();
            }
        }, 2000);
        refresh_id.setLoadmoreFinished(false);
        refresh_id.finishRefresh();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
